package at.jku.se.fastsell.rest;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import at.jku.se.fastsell.data.Auction;
import at.jku.se.fastsell.data.DataAccess;

@Path("/auctions")
public class AuctionResource {

	private static final Logger LOG = Logger.getLogger(AuctionResource.class.getName());

	// ----------------------------------------------------------------------------------

	@GET
	@Path("/{auctionId}/")
	@Produces({ "application/json" })
	public AuctionJSON getAuction(@PathParam("auctionId") String auctionIdString) {
		try {
			long auctionId = Long.parseLong(auctionIdString);
			Auction a = DataAccess.getAuctionByID(auctionId);
			return convertAuction(a);
		} catch (Exception e) {
			LOG.warning("Could not get auction with ID '" + auctionIdString + "': " + e.toString());
			return null;
		}
	}

	@GET
	@Produces({ "application/json" })
	public List<AuctionJSON> getAllAuctions() {
		List<Auction> auctions = DataAccess.getAllAuctions();
		List<AuctionJSON> result = new LinkedList<AuctionJSON>();

		for (Auction a : auctions) {
			result.add(convertAuction(a));
		}
		return result;
	}
	
	private AuctionJSON convertAuction(Auction a) {
		String title = a.getTitle();
		String description = a.getDescription();
		String seller = a.getSeller().getName();
		
		long start = a.getStart().getTime();
		long end = a.getEnd().getTime();
		
		double curPrice = a.getCurPrice();
		long auctionID = a.getAuctionID();
		int bidCount = a.getBidCount();

		String highestBidder;
		if (a.getHighestBidder() != null) {
			highestBidder = a.getHighestBidder().getName();
		} else {
			highestBidder = "";
		}
		
		return new AuctionJSON(auctionID, title, description, highestBidder, curPrice, seller, start, end, bidCount);
	}

	// ----------------------------------------------------------------------------------

	@POST
	@Path("/{auctionId}/placeBid")
	@Produces("text/plain")
	public Response placeBid(@PathParam("auctionId") String auctionIdString, @QueryParam("username") String username,
			@QueryParam("password") String password, @QueryParam("bidprice") String bidprice) {
		String response = "";
		// --
		try {
			long auctionId = Long.parseLong(auctionIdString);
			Auction auction = DataAccess.getAuctionByID(auctionId);
			// --
			if (DataAccess.verifyUser(username, password)) {
				DataAccess.placeBid(auction, Double.parseDouble(bidprice), username);
				LOG.info("Bid successfully placed");
				response = "OK";
			} else {
				response = "Could not place bid: Wrong credentials for " + username;
				LOG.warning(response);
			}
		} catch (Exception e) {
			response = "Could not place bid: " + e.toString();
			LOG.warning(response);
		}
		// --
		return RestHelper.buildResponse(response);
	}

	@POST
	@Path("/create")
	@Produces("text/plain")
	public Response createAuction(@QueryParam("title") String title, @QueryParam("description") String description,
			@QueryParam("startingPrice") String startinPriceString, @QueryParam("duration") String durationString,
			@QueryParam("username") String username, @QueryParam("password") String password) {
		String response = "";
		// --
		try {
			if (DataAccess.verifyUser(username, password)) {
				double startingPrice = Double.parseDouble(startinPriceString);
				int duration = Integer.parseInt(durationString);

				Auction a = DataAccess.createAuction(title, description, username, startingPrice, duration);
				LOG.info("Auction '" + a.getAuctionID() + "' successfully created");
				response = "OK";
				// --
			} else {
				response = "Could not create auction: Wrong credentials for " + username;
				LOG.warning(response);
			}
		} catch (Exception e) {
			response = "Could not create auction: " + e.toString();
			LOG.warning(response);
		}
		// --
		return RestHelper.buildResponse(response);
	}
}
